#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   maker.py
@Time    :   2024/01/16 16:11:14
@Author  :   chakcy 
@Email   :   947105045@qq.com
@description   :   工厂函数
'''

import sys
import os
sys.path.append("../")
from reptile import Reptile
from excel import Excel
from init.init_driver import init_chrome_driver

# 工厂函数，用于构建爬虫对象
def Maker(params, driver_path):
    """
    params['class']: 爬虫类
    params['username']: 用户名账号
    params['password']: 用户名密码
    params['key']: 爬取的岗位名称
    params['pages']: 爬取页数
    params['db_name']: 数据库名称
    params['path']: sqlite 文件路径
    """
    dataPath = os.path.join(params['path'], "../", params['db_name'])
    instance: Reptile = params['class'][0](dataPath, driver_path)
    instance.login(username=params['username'], password=params['password'])
    instance.getUrl(params['key'], int(params['pages']))
    instance.run()
    instance.save()
    instance.close()
    instance_to_excel: Excel = params['class'][1](params['path'], params['db_name'], params['class'][0].__name__)
    instance_to_excel.dataToExcel()
    instance_to_excel.dataToExcelAll()

if __name__ == "__main__":
    sys.path.append("..") 
    from instance.leqing import LeQing, LeQingTOExcel
    classDict = {0:LeQing, 1:LeQingTOExcel}
    params = {
        'class': [LeQing, LeQingTOExcel],
        'username': "######",
        'password': "######",
        'key': '普工',
        'pages': 1,
        'db_name': 'leqing',
        'path': './'
    }
    Maker(params)
